package com.krtech.wecard.module.pub.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.krtech.wecard.framework.exception.BusinessException;
import com.krtech.wecard.framework.model.CommRes;
import com.krtech.wecard.framework.utils.LoginUtil;
import com.krtech.wecard.module.pub.dto.CardInfoVo;
import com.krtech.wecard.module.pub.entity.CardInfo;
import com.krtech.wecard.module.pub.entity.CardInfoUser;
import com.krtech.wecard.module.pub.service.CardInfoService;
import com.krtech.wecard.module.pub.service.CardInfoUserService;
import com.krtech.wecard.module.sys.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/pub/cardInfo")
@Api(value = "卡管理",tags = "卡管理")
public class CardInfoController {

    @Autowired
    private CardInfoService cardInfoService;


    @Autowired
    private CardInfoUserService cardInfoUserService;

    /**
     * 增
     */
    @PutMapping("/add")
    @ApiOperation(value = "新增")
    public CardInfoVo add(@RequestBody CardInfoVo cardInfoVo){
        if (StrUtil.isEmpty(cardInfoVo.getCoverUrl())){
            throw new BusinessException("必填参数code不能不为空");
        }
        SysUser userInfo = LoginUtil.getUserInfo();
        cardInfoVo.setCreatedBy(userInfo.getUserId().toString());
        cardInfoVo.setCreatedTime(new Date());

        return cardInfoService.add(cardInfoVo);
    }

    /**
     * 改
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新")
    public CardInfoVo updateByCardInfoVo(@RequestBody CardInfoVo cardInfoVo){
        if (StrUtil.isEmpty(cardInfoVo.getCoverUrl())){
            throw new BusinessException("必填参数code不能不为空");
        }

        return cardInfoService.updateByCardInfoVo(cardInfoVo);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除")
    public Boolean delete( Integer id){
        if (id == null){
            throw new BusinessException("必填参数id不能不为空");
        }

        return cardInfoService.delete(id);
    }

    /**
     * getById
     * @param id
     * @return
     */
    @GetMapping("/getById")
    @ApiOperation(value = "getById")
    public CardInfoVo getCardInfoById(Integer id){
        if (id == null){
            throw new BusinessException("必填参数id不能不为空");
        }

        return cardInfoService.getCardInfoById(id);
    }

    @GetMapping("/getManageById")
    @ApiOperation(value = "getManageById")
    public CardInfoVo getManageById(Integer id){
        if (id == null){
            throw new BusinessException("必填参数id不能不为空");
        }

        return cardInfoService.getManageById(id);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public Page<CardInfo> page(Page<CardInfo> page, String keyword){
        QueryWrapper<CardInfo> wrapper = new QueryWrapper<>();
        wrapper.eq(CardInfo.COL_DEL_FLAG,0);
        if (StrUtil.isNotEmpty(keyword)){
            wrapper.like(CardInfo.COL_CARD_NAME,keyword);
        }
        Page<CardInfo> managementPage = cardInfoService.page(page, wrapper);
        return managementPage;
    }

    @GetMapping("/list")
    @ApiOperation(value = "list")
    public List<CardInfo> list(String keyword){
        QueryWrapper<CardInfo> wrapper = new QueryWrapper<>();
        wrapper.eq(CardInfo.COL_DEL_FLAG,0);
        if (StrUtil.isNotEmpty(keyword)){
            wrapper.like(CardInfo.COL_CARD_NAME,keyword);

        }
        List<CardInfo> list = cardInfoService.list(wrapper);
        return list;
    }

    /**
     * 判断当前用户是否领卡
     *
     * @return
     */
    @GetMapping("/judgeGetCard")
    @ApiOperation(value = "判断当前用户是否领卡")
    public CommRes judgeGetCard(){
        SysUser userInfo = LoginUtil.getUserInfo();
        int count = cardInfoUserService.count(new QueryWrapper<CardInfoUser>()
                .eq(CardInfoUser.COL_USER_ID, userInfo.getUserId()));
        if (count <= 0){
            return CommRes.error("401","当前用户暂未领卡");
        }
        return CommRes.ok();
    }

    /**
     * 有卡返回信息，没卡领卡后返回信息
     * @return
     */
    @GetMapping("/userGetMyCard")
    @ApiOperation(value = "有卡返回信息，没卡领卡后返回信息")
    public CommRes userGetMyCard(){
        SysUser userInfo = LoginUtil.getUserInfo();
        CardInfoVo cardInfoVo = cardInfoService.userGetMyCard(userInfo.getUserId().intValue());

        return CommRes.ok(cardInfoVo);
    }

    //todo 给定

}
